const express = require('express');

const activityService = require('../service/activity');

const router = express.Router();

// 懒加载获取活动列表
router.get('/activity-list', async function (req, res, next) {
    let uid = req.uid;
    let cursor = req.query.cursor || null;
    let limit = parseInt(req.query.limit) || 20;
    if (limit > 50) {
        limit = 50;
    }
    activityService.getActivityList(uid, cursor, limit).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 参加活动接口
router.post('/join-activity', async function (req, res, next) {
    let uid = req.uid;
    let activityId = parseInt(req.body.activityId);
    if (!activityId) {
        res.status(400).json({ code: 400, msg: '参数错误', data: null });
        return;
    }
    activityService.joinActivity(uid, activityId).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 取消活动接口
router.post('/cancel-activity', async function (req, res, next) {
    let uid = req.uid;
    let activityId = parseInt(req.body.activityId);
    if (!activityId) {
        res.status(400).json({ code: 400, msg: '参数错误', data: null });
        return;
    }
    activityService.cancelActivity(uid, activityId).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 获取活动详情接口
router.get('/activity', function (req, res, next) {
    let uid = req.uid;
    let activityId = parseInt(req.query.id);
    if (!activityId) {
        res.status(400).json({ code: 400, msg: '参数错误', data: null });
        return;
    }
    activityService.getActivityInfo(uid, activityId).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 懒加载已报名活动接口
router.get('/joined-activity-list', async function (req, res, next) {
    let uid = req.uid;
    let cursor = req.query.cursor || null;
    let limit = parseInt(req.query.limit) || 20;
    if (limit > 50) {
        limit = 50;
    }
    activityService.getJoinedActivityList(uid, cursor, limit).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

module.exports = router;